package org.bedracket.pgeg.init;

import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RegisterKeyMappingsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.bedracket.pgeg.PGEGMod;
import org.lwjgl.glfw.GLFW;

@Mod.EventBusSubscriber(value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
public enum EModKeyMappings {

    OPEN_TEST_MENU("open_test_menu", GLFW.GLFW_KEY_O);

    private KeyMapping keyMapping;
    private final String description;
    private final int key;

    private EModKeyMappings(String description, int defaultKey) {
        this.key = defaultKey;
        this.description = PGEGMod.MOD_ID + ".keyinfo." + description;
    }

    @SubscribeEvent
    public static void register(RegisterKeyMappingsEvent event) {
        for (EModKeyMappings key : values()) {
            key.keyMapping = new KeyMapping(key.description, key.key, PGEGMod.MOD_ID);
            event.register(key.keyMapping);
        }
    }

    public KeyMapping getKeyMapping() {
        return keyMapping;
    }

    public String getDescription() {
        return description;
    }

    public int getKey() {
        return keyMapping.getKey().getValue();
    }

    public boolean isPressed() {
        return keyMapping.isDown();
    }

    public String getBoundKey() {
        return keyMapping.getTranslatedKeyMessage()
                .getString()
                .toUpperCase();
    }

    public int getBoundCode() {
        return keyMapping.getKey()
                .getValue();
    }

    public static boolean isKeyDown(int key) {
        return InputConstants.isKeyDown(Minecraft.getInstance()
                .getWindow()
                .getWindow(), key);
    }

    public static boolean isMouseButtonDown(int button) {
        return GLFW.glfwGetMouseButton(Minecraft.getInstance()
                .getWindow()
                .getWindow(), button) == 1;
    }

    public static boolean ctrlDown() {
        return Screen.hasControlDown();
    }

    public static boolean shiftDown() {
        return Screen.hasShiftDown();
    }

    public static boolean altDown() {
        return Screen.hasAltDown();
    }

}